package com.zmn.oms.zmn.aop;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.common.annotation.OrderWorkCasOperate;
import com.zmn.oms.common.dto.OmsBaseOperator;
import com.zmn.oms.common.exception.OmsBusinessException;
import com.zmn.oms.model.dto.work.modify.OrderDistributeDTO;
import com.zmn.oms.services.interfaces.work.OrderWorkCasOperateService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 跟单操作一致性检查
 */
@Aspect
@Component
@Order(-1)
public class OrderWorkCasOperateAspect {

    @Autowired
    private OrderWorkCasOperateService orderWorkCasOperateService;

    /**
     * 设定切入点
     */
    @Pointcut("@annotation(com.zmn.oms.common.annotation.OrderWorkCasOperate)")
    private void pointCutMethod() {
    }

    @Before("pointCutMethod() && @annotation(orderWorkCasOperate)")
    public void doProcess(JoinPoint joinPoint, OrderWorkCasOperate orderWorkCasOperate) throws OmsBusinessException {
        OmsBaseOperator omsBaseOperator = (OmsBaseOperator) joinPoint.getArgs()[0];

        // 后台操作才处理
        if (!Objects.equals(omsBaseOperator.getOperatorType(), GlobalConsts.OPERATE_USER_TYPE_STAFF)) {
            return;
        }

        if (StringUtils.isEmpty(omsBaseOperator.getCasSign())) {
            return;
        }

        // 数据验证
        orderWorkCasOperateService.validate(omsBaseOperator, orderWorkCasOperate.type());
    }

}
